package com.marvels.financial.loan.controller;

import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.dto.McfeLoanWithholdingDetailDTO;
import com.marvels.financial.loan.service.RepaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
@RequestMapping("financial/mcfe/service")
public class RepaymentContraller {
	@Autowired
	private RepaymentService repaymentService;

	/**查询咨询费代扣流水详情
	 * @author 娄凯琦
	 * @date 2018年5月18日  
	 * @return
	 */
	@RequestMapping("/queryLoanWithholdingDetail")
	public MarvelsResultDTO queryLoanWithholdingDetail(@RequestBody(required=false)McfeLoanWithholdingDetailDTO mcfeLoanWithholdingDetailDTO){
		try {
			MarvelsLogUtil.getInstance().info("queryLoanWithholdingDetail the method start ==>" + mcfeLoanWithholdingDetailDTO);
			List<McfeLoanWithholdingDetailDTO> list = repaymentService.searchLoanWithholdingDetail(mcfeLoanWithholdingDetailDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),list);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(e.getMessage(), e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error("查询咨询费代扣流水详情异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}
	
	
	
	/**人工收取咨询费
	 * @author 娄凯琦
	 * @date 2018年5月19日  
	 * @param mcfeLoanWithholdingDetailDTO
	 * @return
	 */
	@RequestMapping("/manualLoanWithholding")
	public MarvelsResultDTO manualLoanWithholding(@RequestBody(required=false)McfeLoanWithholdingDetailDTO mcfeLoanWithholdingDetailDTO){
		try {
			MarvelsLogUtil.getInstance().info("manualLoanWithholding the method start ==>" + mcfeLoanWithholdingDetailDTO);
			repaymentService.manualServiceCharge(mcfeLoanWithholdingDetailDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),"收取成功");
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(e.getMessage(), e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error("人工收取咨询费异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}
	
	
	
	/**重新发起咨询费代扣
	 * @author 娄凯琦
	 * @date 2018年5月19日  
	 * @param mcfeLoanWithholdingDetailDTO
	 * @return
	 */
	@RequestMapping("/reWithholding")
	public MarvelsResultDTO reWithholding(@RequestBody(required=false)McfeLoanWithholdingDetailDTO mcfeLoanWithholdingDetailDTO){
		try {
			MarvelsLogUtil.getInstance().info("manualLoanWithholding the method start ==>" + mcfeLoanWithholdingDetailDTO);
			repaymentService.withHoldingAgain(mcfeLoanWithholdingDetailDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),"发起成功");
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(e.getMessage(), e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error("重新发起咨询费代扣异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}
	
	
	
	/**查询交易结果
	 * @author 娄凯琦
	 * @date 2018年5月19日  
	 * @param mcfeLoanWithholdingDetailDTO
	 * @return
	 */
	@RequestMapping("/reQueryWithholding")
	public MarvelsResultDTO reQueryWithholding(@RequestBody(required=false)McfeLoanWithholdingDetailDTO mcfeLoanWithholdingDetailDTO){
		try {
			MarvelsLogUtil.getInstance().info("manualLoanWithholding the method start ==>" + mcfeLoanWithholdingDetailDTO);
			repaymentService.querywithHoldingResult(mcfeLoanWithholdingDetailDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),"查询成功");
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(e.getMessage(), e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error("查询咨询费代扣交易结果异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}
}
